
import { Component, OnInit } from '@angular/core';
import { User } from '../../domain/user';

import { AuthService } from '../../service/auth.service';
import { UserService } from '../../service/user.service';
import { Router } from '@angular/router';
/*

@Author:李朝晖

@Date: 2018-12-12

@登录功能实现

@Last Modified time: 2018-12-16
*/
@Component({
  selector: 'app-login-main',
  templateUrl: './login-main.component.html',
  styleUrls: ['./login-main.component.css']
})
export class LoginMainComponent {

  constructor(private userService: UserService, private _router: Router) { }

  user: User;
  private inform: string;
  private password: string = "";


  getUser(value: string): void {
    console.log(value);
    this.userService.getUser(value)
      .subscribe(user => {
        if (user.length == 0) {
          this.user = null;
          this.updateInform("当前用户不存在！");
        } else {
          this.user = user[0];
          this.updateInform(this.user.position + "  " + this.user.name + " 您好！");
        }
      });
  }

  loginConfirm(): void {
    this.userService.getUserConfirm(this.user.id, this.password)
      .subscribe(userConfirm => {
        if (userConfirm.length == 0) {
          this.updateInform("密码输入不正确！请重新输入...");
        } else {
          this.updateInform("登录成功！请稍等...(●'◡'●)");
          AuthService.login(this.user.id, this.user.position);
          if (this.user.position == "员工") {
            this.jump('/staff');
          } else if (this.user.position == "行政人员") {
            this.jump('/administrative');
          } else if (this.user.position == "部门经理") {
            this.jump('manager');
          } else if (this.user.position == "副总经理") {
            this.jump('manager');
          }else if (this.user.position == "总经理") {
            this.jump('manager');
          }else {
            this.updateInform("跳转出错！！！");
          }

        }
      });
  }

  updateInform(value: string) {
    if (this.inform == value) {
      this.inform = value + '#';
    } else {
      this.inform = value;
    }
  }

  jump(url: string) {
    setTimeout(() => {
      this._router.navigateByUrl(url);
      // 或者执行_router.navigate(['/a']) 两者机理一致
    }, 1000);
  }
  
}
